<template>
  <div id='statistics'>
    <v-header title="统计分析" :saveShow="false" :showDownloadBtn="false"></v-header>
    <div class="main-cont">
      <!--问题统计-->
      <el-card class="version">
        <div slot="header" class="clearfix">
          <el-form :inline="true">
            <el-form-item label="应用版本">
              <el-select v-model="versionId" placeholder="请选择" @change="handleVersionSelect">
                <el-option
                    label="全部"
                    value=""
                ></el-option>
                <el-option v-for="item in versionList"
                           :key="item.ver_id"
                           :label="item.ver_num"
                           :value="item.ver_num"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="统计周期">
              <el-select v-model="dateType" placeholder="请选择" @change="handleDateTypeSelect">
                <el-option v-for="item in dateArr"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="解决状态">
              <el-select v-model="exp_status" placeholder="请选择" @change="handleStatusValSelect">
                <el-option v-for="item in statusValArr"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <el-row :gutter="30" class="bug-box">
          <el-col :span="6" v-for="(item, index) in bugObj" :key="index"
                  @click.native="handleExpType(item.exp_type, index)">
            <el-tooltip class="item" effect="dark" :content="cardTips[index]" placement="bottom">
              <el-card shadow="hover" :class="{'active': currIndex === index}">
                <p>{{ item.counts }}</p>
                <span v-if="item.exp_type === '0'">异常</span>
                <span v-if="item.exp_type === '1'">崩溃</span>
                <span v-if="item.exp_type === '2'">卡顿</span>
                <span v-if="item.exp_type === '-1'">总计</span>
              </el-card>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-card>
      <!-- 今日数据 -->
      <div class="day-data-box">
        <el-row :gutter="10">
          <el-col :span="12">
            <div class="tags" style="margin-top: 10px">
              <v-pageHeader title="数据分析"></v-pageHeader>
            </div>
            <div class="problem-details">
              <div class="item">
                <strong>已解决</strong>
                <span>{{ dataDetails.dealed ? dataDetails.dealed : 0 }}</span>
              </div>
              <div class="item">
                <strong>未解决</strong>
                <span>{{ dataDetails.undeal ? dataDetails.undeal : 0 }}</span>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="tags" style="margin-top: 10px">
              <v-pageHeader title="热门数据"></v-pageHeader>
            </div>
            <div class="day-top-data">
              <ul v-if="expDataList.length > 0">
                <li v-for="item in expDataList" :key="item.key">
                  {{ item.exp_title }} <span class="count"> {{ item.bug_happen_count }}</span>
                </li>
              </ul>
              <div class="no_record" v-else>
                <span>暂无数据哦</span>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      <!--趋势分析-->
      <el-card style="margin-top:10px">
        <div slot="header" class="clearfix">
          <span>趋势分析({{ dateText }})</span>
        </div>
        <div class="line-chart">
          <chart :options="option"></chart>
        </div>
      </el-card>
      <!--数据分布-->
      <div class="pie-box" style="min-height: 460px">
        <div class="title">
          <span>数据分布</span>
        </div>
        <div class="pie-chart">
          <!-- <el-row :gutter="24">
            <el-col :span="11">
              <vPie :dataOpt="deviceData" title="设备分布"></vPie>
            </el-col>
            <el-col :span="1">
              <div class="class10px"></div>
            </el-col>
            <el-col :span="11">
              <vPie :dataOpt="versionData" title="版本分布"></vPie>
            </el-col>
          </el-row> -->
          <div class="float-l mg-r10">
            <vPie :dataOpt="deviceData" title="设备分布"></vPie>
          </div>
          <div class="float-l">
            <vPie :dataOpt="versionData" title="版本分布"></vPie>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
  import {
    VHeader,
    VPageHeader
  } from './../../components'
  import vPie from './comm/vPie'
  import 'echarts/lib/component/tooltip'
  import {nextPage} from '../../assets/js/index'
  import {mixinParams} from '../../assets/js/mixin1'
  import api from '../../api/url'
  import ajax from '../../api/ajax/ajax'
  // import vue from 'vue'
  export default {
    components: {
      VHeader,
      VPageHeader,
      vPie
    },
    mixins: [mixinParams],
    data () {
      return {
        // abnormal: '', // 在折线图中鼠标放上显示对应的异常名称
        deviceData: [],
        versionData: [],
        option: {
          grid: {
            top: 10,
            bottom: 0,
            left: 0,
            right: 20,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              lineStyle: {
                color: '#fff',
                opacity: 0.5
              }
            },
            position: function (pos, params, dom, rect, size) {
              return [pos[0] + 10, pos[1] + 10]
            },
            formatter (params) {
              return `(${params[0].name}) 次数：${params[0].value}`
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['07-02', '07-03', '07-04', '07-05', '07-06', '07-07', '07-08', '07-09', '07-10', '07-11', '07-12', '07-13', '07-14', '07-15'],
            axisTick: {
              show: false
            }
          },
          yAxis: {
            type: 'value',
            axisTick: {
              show: false
            }
          },
          lineStyle: {
            width: 0.1,
            color: '#419df7'
          },
          series: [{
            data: [0, 4, 1, 0, 2, 0, 0, 0, 1, 9, 6, 0, 0, 0],
            type: 'line',
            itemStyle: {
              normal: {
                color: '#419df7'
              }
            }
          }]
        },
        versionId: '',
        versionList: null,
        bugObj: null,
        cardTips: ['系统异常次数的总和', '系统崩溃次数的总和', '系统卡顿次数的总和', '系统出现问题的总和'],
        dateType: '2',
        dateText: '前十五天',
        dateArr: [
          {value: '1', label: '前七天'},
          {value: '2', label: '前十五天'},
          {value: '3', label: '今天'},
          {value: '4', label: '昨天'}
        ],
        currIndex: -1,
        expType: '-1',
        exp_status: '',
        statusValArr: [
          {value: '', label: '全部'},
          {value: '0', label: '未解决'},
          {value: '1', label: '已解决'}
        ],
        dataDetails: {},
        expDataList: []
      }
    },
    watch: {
      appFlag: {
        handler (val) {
          if (val) {
            this.addFlag = true
            nextPage('/home/bugIndex')
            this.pd_updatePath('/home/bugIndex')
          }
        }
      }
    },
    methods: {
      //  查询版本列表
      queryVersionList () {
        let params = {
          ver_select: '1'
        }
        ajax.post(api.SELECT_APP_VERSION_LIST, params).then(res => {
          this.versionList = res.data.list
        })
      },
      //  查询异常数目
      queryBugInfoCount () {
        let params = {
          app_ver: this.versionId,
          exp_types: [],
          date_type: this.dateType,
          exp_status: this.exp_status
        }
        ajax.post(api.SELECT_BUG_INFO_COUNT, params).then(res => {
          this.bugObj = res.data.data_list
        })
      },
      // 查询异常折线图
      queryBugInfoDateChart () {
        let params = {
          app_ver: this.versionId,
          date_type: this.dateType,
          exp_types: [],
          exp_status: this.exp_status
        }
        if (this.expType !== '-1') {
          params.exp_types.splice(0, 1, this.expType)
        }
        ajax.post(api.SELECT_BUG_INFO_DATE_CHART, params).then(res => {
          this.option.xAxis.data = res.data.xaxis
          this.option.series[0].data = res.data.series
        })
      },
      // 查询设备分布信息
      queryDevicePieChart () {
        let params = {
          app_ver: this.versionId,
          exp_types: [],
          date_type: this.dateType,
          exp_status: this.exp_status
        }
        if (this.expType !== '-1') {
          params.exp_types.splice(0, 1, this.expType)
        }
        ajax.post(api.SELECT_DEVICE_PIE_CHART, params).then(res => {
          this.deviceData = res.data
        })
      },
      // 查询版本分布信息
      queryVersionPieChart () {
        let params = {
          exp_types: [],
          date_type: this.dateType,
          exp_status: this.exp_status,
          app_ver: this.versionId
        }
        if (this.expType !== '-1') {
          params.exp_types.splice(0, 1, this.expType)
        }
        ajax.post(api.SELECT_VERSION_PIE_CHART, params).then(res => {
          this.versionData = res.data
        })
      },
      // 切换版本
      handleVersionSelect (val) {
        this.versionId = val
        this.expType = '-1'
        this.currIndex = -1
        this.queryBugInfoCount()
        this.queryBugInfoDateChart()
        this.queryDevicePieChart()
        this.queryVersionPieChart()
        this.queryBugCountForStatus()
      },
      // 选择版本周期
      handleDateTypeSelect (val) {
        this.dateType = val
        this.expType = '-1'
        this.currIndex = -1
        this.dateArr.forEach((item) => {
          if (item.value === val) {
            this.dateText = item.label
          }
        })
        this.queryBugInfoCount()
        this.queryBugInfoDateChart()
        this.queryDevicePieChart()
        this.queryVersionPieChart()
        this.queryBugCountForStatus()
      },
      // 选择异常类型
      handleExpType (expType, index) {
        if (this.currIndex !== index) {
          this.currIndex = index
          this.expType = expType
        } else {
          this.currIndex = -1
          this.expType = '-1'
        }
        this.queryBugInfoDateChart()
        this.queryDevicePieChart()
        this.queryVersionPieChart()
        this.queryBugCountForStatus()
      },
      // 状态
      handleStatusValSelect (val) {
        this.exp_status = val
        this.queryBugInfoCount()
        this.queryBugInfoDateChart()
        this.queryDevicePieChart()
        this.queryVersionPieChart()
        this.queryBugCountForStatus()
      },
      // 查询今日数据
      queryBugCountForStatus () {
        let params = {
          date_type: this.dateType,
          exp_status: this.exp_status,
          app_ver: this.versionId,
          exp_types: [this.expType]
        }
        ajax.post(api.SELECT_BUG_COUNT_FOR_STATUS, params).then(res => {
          this.dataDetails = res.data.bug_status_count
          this.expDataList = res.data.bug_top5_title
        })
      }
    },
    mounted () {
    },
    created () {
      this.queryVersionList()
      this.queryBugInfoCount()
      this.queryBugInfoDateChart()
      this.queryDevicePieChart()
      this.queryVersionPieChart()
      this.queryBugCountForStatus()
    }
  }
</script>
<style lang='stylus' rel='stylesheet/stylus' type="text/stylus">
  #statistics
    height 100%
    .main-cont
      overflow: auto
      margin 10px 0 0 10px
      height calc(100% - 56px)
      padding-right 10px
      & > .el-card
        border 0 none
      .pie-box
        margin-top 10px
        border-radius 4px
        box-shadow 0 2px 12px 0 rgba(0, 0, 0, .1)
        .title
          height 48px
          padding 14px 20px
          line-height 20px
          background #ffffff
          border-bottom 1px solid #ebeef5
    .el-card__header
      padding 14px 20px
      .el-form-item
        margin 0
        .el-form-item__label
          font-size 13px
      .el-select
        .el-input__inner
          width 180px
          line-height 30px
          font-size 13px
          padding 0 !important
          border 0 none
          border-radius 0
          border-bottom 1px solid #dcdfe6
          &:focus
            border-color #016DFB
    .version .el-card__header
      padding 11px 20px
      .el-form-item
        margin-right 40px
    .el-dropdown-link
      cursor pointer
    .el-card__body
      padding 25px 20px !important
      overflow hidden
      .el-card__body
        height 100px
        text-align center
        padding 15px
        font-size 13px
        color #5e5e5e
        -webkit-font-smoothing: antialiased
        & > p
          font-size 32px
          line-height 32px
          color #000
          -webkit-font-smoothing: initial
        & > span
          display block
          line-height 28px
    .line-chart
      width 100%
      height 300px
      .echarts
        height 100%
        margin 0 auto
        width 1102px
    .pie-chart
      &:after
        content '.'
        height 0
        display block
        overflow hidden
        clear both
      .echarts
        canvas
          width 100%
          height 100% !important
      .float-l
        width calc(50% - 10px) !important
        height 100% !important
        float left
        padding-bottom 70px
        background #ffffff
      .mg-r10
        margin-right 10px
    .bug-box
      .el-col-6
        height auto
        & > .el-card
          &.active
            box-shadow 0 0 8px rgba(1, 109, 251, 0.6)
    .tags
      height 51px
      border-bottom 1px solid #d8dce5
      .font12
        padding-left 20px
        font-size 14px
    .day-data-box
      .problem-details
        background #fff
        padding 20px 50px
        text-align center
        overflow hidden
        .item
          float left
          width 50%
          height 150px
          padding 48px 0 47px
          position relative
          strong
            display block
            font-size 20px
            line-height 24px
            font-weight normal
          span
            display block
            margin-top 5px
            font-size 26px
            line-height 26px
            color #67c23a
          &:last-child
            span
              color #f56c6c
          &:before
            content ''
            position absolute
            top 50%
            left 50%
            width 150px
            height 150px
            margin -75px 0 0 -75px
            border 15px solid #67c23a
            border-radius 50%
            box-sizing border-box
            box-shadow 0 0 10px rgba(103, 194, 58, 0.5), inset 0 0 10px rgba(103, 194, 58, 0.3)
          &:last-child:before
            border-color #f56c6c
            box-shadow 0 0 10px rgba(245, 108, 108, 0.5), inset 0 0 10px rgba(245, 108, 108, 0.3)
      .day-top-data
        height 190px
        background #fff
        padding 0 15px
        overflow hidden
        ul
          li
            height 38px
            line-height 38px
            white-space nowrap
            text-overflow ellipsis
            padding-right 40px
            border-bottom 1px dashed #d8dce5
            overflow hidden
            position relative
            &:last-child
              border-bottom 0 none
            .count
              position absolute
              right 0
              top 0
              height 38px
              color #999
    .no_record
      padding 20px
      span
        display block
        line-height 48px
        height 48px
        font-size 14px
        color #999
        text-align center
        position relative
        &:before
          content '\e658'
          display inline-block
          vertical-align top
          height 48px
          font-size 48px
          margin-right 5px
          color #999
          font-family 'iconfont'
  .el-tooltip__popper.is-dark
    opacity 0.5
</style>
